package com.spzx.cart.controller;

import com.spzx.cart.api.domain.CartInfo;
import com.spzx.cart.service.ICartService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;


import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.RequiresLogin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author ChenXing
 * @Description
 * @Create 2024/8/13 11:41
 */

@Tag(name = "购物车接口")
@RestController
@RequestMapping
public class CartController extends BaseController {
    @Autowired
    private ICartService cartService;

    @Operation(summary = "添加购物车")
    @RequiresLogin
    @GetMapping("addToCart/{skuId}/{skuNum}")
    public AjaxResult addToCart( @PathVariable("skuId") Long skuId, @PathVariable("skuNum") Integer skuNum) {
        cartService.addToCart(skuId, skuNum);
        return success();
    }

    @Operation(summary = "查询购物车")
    @RequiresLogin
    @GetMapping("cartList")
    public AjaxResult cartList() {
      List<CartInfo> cartInfoList = cartService.getCartList();
        return success(cartInfoList);
    }


    @Operation(summary = "删除购物车")
    @RequiresLogin
    @DeleteMapping("deleteCart/{skuId}")
    public AjaxResult deleteCart(@PathVariable(("skuId"))  Long skuId) {
       cartService.deleteCart(skuId);
        return success();
    }

    @Operation(summary = "更新购物车商品选中状态")
    @RequiresLogin
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public AjaxResult checkCart(@PathVariable("skuId" ) Long skuId, @PathVariable("isChecked") Integer isChecked) {
        cartService.checkCart(skuId, isChecked);
        return success();
    }

    @Operation(summary="更新购物车商品全部选中状态")
    @RequiresLogin
    @GetMapping("allCheckCart/{isChecked}")
    public AjaxResult allCheckCart(@Parameter(name = "isChecked", description = "是否选中 1:选中 0:取消选中", required = true) @PathVariable(value = "isChecked") Integer isChecked){
        cartService.allCheckCart(isChecked);
        return success();
    }

    @Operation(summary="清空购物车")
    @RequiresLogin
    @GetMapping("clearCart")
    public AjaxResult clearCart(){
        cartService.clearCart();
        return success();
    }


    @Operation(summary="获得购物车选中列表")
    @RequiresLogin
    @GetMapping("getCartCheckedList/{userId}")
    R<List<CartInfo>> getCartCheckedList(@PathVariable("userId") Long userId){
        List<CartInfo> cartInfoList =  cartService.getCartCheckedList(userId);
        return R.ok(cartInfoList);
    }



    @Operation(summary="清空被选中购物车")
    @RequiresLogin
    @GetMapping("/deleteCartCheckedList/{userId}")
    R<Boolean> deleteCartCheckedList(@PathVariable("userId") Long userId){
       Boolean b =  cartService.deleteCartCheckedList(userId);
        return R.ok(b);
    }

    @Operation(summary="更新购物车价格信息")
    @RequiresLogin
    @GetMapping("/updateCartPrice/{userId}")
    void updateCartPrice(@PathVariable("userId") Long userId){
        cartService.updateCartPrice(userId);
    }



}
